package top.huiwow.managers;

import net.md_5.bungee.api.ChatColor;
import org.bukkit.Bukkit;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Score;
import org.bukkit.scoreboard.Scoreboard;
import top.huiwow.GameCore;

public class HealthManager implements Listener {

    private final GameCore plugin;
    Boolean noNameTagAPI = null;

    public HealthManager() {
        this.plugin = GameCore.getInstance();
    }

    private void healthSetup(Player player) {


        final Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();

        if (board.getObjective("health") != null) {
            board.getObjective("health").unregister();
        }

        final Objective obj = board.registerNewObjective("hp", "health");
        obj.setDisplayName(ChatColor.RED + "HP");
        obj.setDisplaySlot(DisplaySlot.BELOW_NAME);

        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () -> {
            for (Player others : Bukkit.getOnlinePlayers()) {
                Score health = obj.getScore(others.getName());

                if (health.getScore() == 0) {
                    health.setScore((int) others.getHealth());
                }
            }
        }, 1);

        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () -> player.setScoreboard(board), 2);
    }

    @EventHandler
    public void onJoin(PlayerJoinEvent event) {
        if (noNameTagAPI == null) {
            noNameTagAPI = Bukkit.getPluginManager().getPlugin("NametagAPI") == null;
        }
        if (noNameTagAPI) {
            healthSetup(event.getPlayer());
        }
    }

    @EventHandler
    public void onRespawn(final PlayerRespawnEvent event) {
        if (noNameTagAPI) {
            healthSetup(event.getPlayer());

            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () -> {
                healthSetup(event.getPlayer());
            }, 4);
        }
    }


    public void heal(Player player, double amount) {
        double health = player.getHealth();

        if (health < 40 - amount) {
            player.setHealth(health + amount);
        } else {
            player.setHealth(40);
        }
    }

    public void feed(Player player, int amount) {
        int food = player.getFoodLevel();

        if (food < 20 - amount) {
            player.setFoodLevel(food + amount);
        } else {
            player.setFoodLevel(20);
        }
    }

    public void trueDamage(Player player, double amount, Player damager) {
        double health = player.getHealth();

        if (health >= amount + 0.01) {
            player.damage(0.01, damager);
            player.setHealth(health - amount);
        } else {
            player.damage(250, damager);
        }
    }

    public void trueDamage(LivingEntity entity, double amount, Player damager) {
        double health = entity.getHealth();

        if (health >= amount + 0.01) {
            entity.damage(0.01, damager);
            entity.setHealth(health - amount);
        } else {
            damager.damage(250, damager);
        }
    }
}
